package com.symaster.common.geom.entity;

import java.io.Serializable;

/**
 * @author yinmiao
 * @version 2022-08-23 19:26
 */
public class RotatedRect implements Serializable {
    private Point center;
    private Size size;
    private double angle = 0.0D;

    /**
     * 平移
     *
     * @param vector 移动向量
     */
    public void translate(Vector vector) {
        translate(vector.getX(), vector.getY());
    }

    /**
     * 平移
     */
    public void translate(double xn, double yn) {
        if (xn == 0.0D && yn == 0.0D) {
            return;
        }
        if (center == null) {
            return;
        }
        center.x = center.x + xn;
        center.y = center.y + yn;
    }

    public Point getCenter() {
        return center;
    }

    public void setCenter(Point center) {
        this.center = center;
    }

    public Size getSize() {
        return size;
    }

    /**
     * 返回向前的延长线
     *
     * @param length 线段长度, 小于等于零为默认长度
     */
    public ExtendedLine extendedLine(double length) {
        double x1 = center.getX() - size.getW() / 2;
        double x2 = center.getX() + size.getW() / 2;
        if (length < 0) {
            length = 3000;
        }

        LineSegment line1 = new LineSegment(new Point(x1, center.getY() - length), new Point(x1, center.getY()));
        LineSegment line2 = new LineSegment(new Point(x2, center.getY() - length), new Point(x2, center.getY()));

        if (angle != 0.0D) {
            line1.rotate(center, angle);
            line2.rotate(center, angle);
        }

        ExtendedLine extendedLine = new ExtendedLine();
        extendedLine.setLines(new LineSegment[]{line1, line2});
        return extendedLine;
    }

    public Double getArea() {
        if (size == null) {
            return null;
        }
        return size.getW() * size.getH();
    }

    public void setSize(Size size) {
        this.size = size;
    }

    public double getAngle() {
        return angle;
    }

    public void setAngle(double angle) {
        this.angle = angle;
    }

    public Polygon toPolygon() {
        if (center == null || size == null) {
            return null;
        }
        double x = center.getX(), y = center.getY(), w = size.getW(), h = size.getH();
        double x1 = x - w / 2, x2 = x + w / 2, y1 = y - h / 2, y2 = y + h / 2;
        double[] xPoint = new double[]{x1, x2, x2, x1};
        double[] yPoint = new double[]{y1, y1, y2, y2};
        Polygon result = new Polygon(xPoint, yPoint);
        if (angle != 0.0D) {
            result.rotateTransform(center, angle);
        }
        return result;
    }

    @Override
    public String toString() {
        return "RotatedRect{" +
                "center=" + center +
                ", size=" + size +
                ", angle=" + Double.parseDouble(String.format("%.4f", angle)) +
                '}';
    }

    public static class ExtendedLine {
        private LineSegment[] lines;

        public LineSegment[] getLines() {
            return lines;
        }

        public void setLines(LineSegment[] lines) {
            this.lines = lines;
        }
    }
}
